﻿layui.use(['layer', 'table', 'laypage', 'laydate', 'jquery'], function () {
    var $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate,
        player = parent.layer || layui.layer;

    var authorityOrganizationListLogic = {
        //初始化
        init: function () {
            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                field: 'iSort',
                order: 'asc',
                listsUrl: "/Authority/Organization/LoadListInfo",
                addUrl: "/Authority/Organization/Add",
                modifyUrl: "/Authority/Organization/Modify",
                auditInfoUrl: "/Authority/Organization/AuditInfo"
            };

            //this.Parameters.ac
            //    .pushQueue(function () {
            //        CommonLogic.loadButtonsInfo(function () {
            //            self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //        });
            //    })
            //    .pushQueue(function () {
            //        self.initPage();
            //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
            //    })
            //    .pushQueue(this.bindEvent)
            //    .exec();

            //页面初始化
            this.initPage();
            //时间初始化
            this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            var title =
                [{ type: 'checkbox'},
                { field: 'gCode', title: 'Code', hide: true },
                { field: 'sName', title: '公司名称', width: '250', align: "center", sort: true },
                { field: 'sAbbreviation', title: '公司简称', width: '150', align: "center", sort: true },
                { field: 'sLeader', title: '联系人', width: '110' },
                { field: 'sPhone', title: '联系电话', width: '120' },
                { field: 'sEmail', title: '邮箱', width: '180' },
                //{ field: 'iSort', title: '排序', width: '70', align: "center" },
                {
                    field: 'bIsEnabled', title: '是否启用', width: '90', align: "center",
                    templet: function (d) {
                        return d.bIsEnabled ? "启用" : "停用";
                    }
                },
                {
                    field: 'iStatus', title: '状态', width: '90', align: "center",
                    templet: function (d) {
                        return GrdEnum.getValue.organizationStatusValue(d.iStatus);
                    }
                },
                { field: 'sContent', title: '备注', width: '300' },
                { title: '操作', toolbar: '#gridTool', width: '230', align: 'left' }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#OrganizationList',
                height: "full-140",
                url: self.Parameters.listsUrl,
                where: {
                    field: self.Parameters.field,
                    order: self.Parameters.order
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "OrganizationList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
        },
        //绑定事件
        bindEvent: function () {

            //监听(列表操作)
            table.on('tool(OrganizationList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnEdit":
                        self.logicFunc.modifyInfo(data);
                        break;
                    case "funBtnRemove":
                        self.logicFunc.removeInfo(data);
                        break;
                    case "funBtnAuditPass":
                        self.logicFunc.auditInfo(1, data);
                        break;
                    case "funBtnAudiFailure":
                        self.logicFunc.auditInfo(2, data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });
            //修改
            $("#funBtnEdit").on("click", function () {
                self.logicFunc.modifyInfo();
            });
            //审核通过
            $("#funBtnAuditPass").on("click", function () {
                self.logicFunc.auditInfo(1);
            });
            //审核失败
            $("#funBtnAudiFailure").on("click", function () {
                self.logicFunc.auditInfo(2);
            });
        },
        //逻辑方法
        logicFunc: {

            ///获取数据
            loadListInfo: function () {
                GrdLayUI.tableReload({
                    domId: "OrganizationList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: $("#txtKeyWord").val()
                });
            },
            //修改按钮信息
            modifyInfo: function (data) {
                if (data != undefined && data != null) {
                    var index = layui.layer.open({
                        title: "修改公司信息",
                        type: 2,
                        resize: false,
                        area: ['600px', '550px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "OrganizationList",
                        backAction: function (data) {
                            var index = layui.layer.open({
                                title: "修改公司信息",
                                type: 2,
                                resize: false,
                                area: ['600px', '550px'],
                                content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(data),
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                        }
                    });
                }
            },
            //审核信息
            auditInfo: function (type, data) {
                if (data != undefined && data != null) {
                    var json = {
                        parameters: {
                            iType: type,
                            gCode: data.gCode
                        }
                    };
                    GrdLayUI.ajaxPost({
                        url: self.Parameters.auditInfoUrl,
                        data: json,
                        success: function (result) {
                            if (result.Code == 1000) {
                                self.logicFunc.loadListInfo();
                            } else {
                                GrdLayUI.msg(result.Msg, "警告");
                            }
                        }
                    });
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "OrganizationList",
                        backAction: function (data) {
                            var json = {
                                parameters: {
                                    iType: type,
                                    gCode: data.gCode
                                }
                            };
                            GrdLayUI.ajaxPost({
                                url: self.Parameters.auditInfoUrl,
                                data: json,
                                success: function (result) {
                                    if (result.Code == 1000) {
                                        self.logicFunc.loadListInfo();
                                    } else {
                                        GrdLayUI.msg(result.Msg, "警告");
                                    }
                                }
                            });
                        }
                    });
                }
            },
        },
        //工具
        tools: {

        }
    };
    authorityOrganizationListLogic.init();
});
